﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using Common;
using System.Windows.Forms;

namespace CommandReceiver
{
    public class Work
    {
        public static EventQueue Events = new EventQueue();
        TcpServer server = null;

        public Work()
        {
            server = new TcpServer(6003);
            server.Accept += new TcpAcceptEventHandler(server_Accept);
            server.Start();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="socket"></param>
        private void server_Accept(Object sender, Socket socket)
        {
            try
            {
                byte[] bs = System.Text.Encoding.Default.GetBytes("ok");
                socket.Send(bs);
                while (true)
                {
                    byte[] receive = new byte[1024];
                    socket.Receive(receive);
                    string str = System.Text.Encoding.Default.GetString(receive);
                    Command command = Parser.Parse(str);
                    switch (command.Type)
                    {
                        case "key":
                            //todo:
                            KeyCommand(command);
                            break;
                        case "mouse":
                            //todo:
                            MouseCommand(command);
                            break;
                        default:
                            break;
                    }
                }
            }
            finally
            {
                socket.Close();
                server.Stop();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="command"></param>
        private void KeyCommand(Command command)
        {
            switch(command.Operation)
            {
                case "down":
                    foreach (var item in command.Commands)
                    {
                        SendKeys.Send(item.Value);
                    }
                    break;
                case "up":
                    foreach (var item in command.Commands)
                    {
                        SendKeys.Send(item.Value);
                    }
                    break;
                default :
                    break;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="command"></param>
        private void MouseCommand(Command command)
        {
            switch (command.Operation)
            {
                case "down":
                    Win32API.MouseDown(Cursor.Current.Handle);
                    break;
                case "up":
                    Win32API.MouseDown(Cursor.Current.Handle);
                    break;
                case "move":
                    Cursor.Position = new System.Drawing.Point(Convert.ToInt32(command.Commands[0]), Convert.ToInt32(command.Commands[1]));
                    break;
                default:
                    break;
            }
        }
    }

}
